from django.test import TestCase

# Create your tests here.

menus = [{'groups__menus__firstmenu': '一级菜单一', 'groups__menus__secondmenu__secondmenu': '一级菜单子菜单1', 'groups__menus__secondmenu__routename': 'rdp'}, {'groups__menus__firstmenu': '一级菜单二', 'groups__menus__secondmenu__secondmenu': '二级菜单子菜单1', 'groups__menus__secondmenu__routename': 'rdp2index'}, {'groups__menus__firstmenu': '一级菜单三', 'groups__menus__secondmenu__secondmenu': '三级菜单子菜单2', 'groups__menus__secondmenu__routename': 'rdp2'}, {'groups__menus__firstmenu': '一级菜单三', 'groups__menus__secondmenu__secondmenu': '三级菜单子菜单1', 'groups__menus__secondmenu__routename': 'rdp3'}, {'groups__menus__firstmenu': None, 'groups__menus__secondmenu__secondmenu': None, 'groups__menus__secondmenu__routename': None}]



# 重组数据
formatted_data = []
for item in menus:
    if item["groups__menus__firstmenu"] != None:
        title = item.pop('groups__menus__firstmenu')
        # 从字典中移除title，剩下的作为子菜单项
        sub_menu_item = {
            'name': item.pop('groups__menus__secondmenu__secondmenu'),
            'route': item.pop('groups__menus__secondmenu__routename')
        }

        # 查找或创建对应的一级菜单项
        menu_item = next((menu for menu in formatted_data if menu['title'] == title), None)
        if menu_item:
            # 如果一级菜单已存在，则添加到其value列表中
            menu_item['value'].append(sub_menu_item)
        else:
            # 否则，创建新的菜单项
            formatted_data.append({'title': title, 'value': [sub_menu_item]})


# print(formatted_data)
# print(menulist)
